package com.example.polardbxdemo.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.polardbxdemo.entity.Material;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MaterialMapper extends BaseMapper<Material> {

    /**
     * 自定义查询：根据类型查询物料
     */
    default List<Material> selectByType(Integer type) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Material::getType, type)
                .eq(Material::getDeleted, 0);
        return selectList(queryWrapper);
    }

    /**
     * 自定义查询：根据JSON字段查询
     * 注意：PolarDB-X 的 JSON 查询语法可能与 MySQL 略有不同
     */
    @Select("SELECT * FROM material WHERE JSON_EXTRACT(properties, '$.category') = #{category} AND deleted = 0")
    List<Material> selectByPropertyCategory(@Param("category") String category);

    /**
     * 使用 LambdaQueryWrapper 查询指定类型的物料并按照创建时间倒序
     */
    default List<Material> selectByTypeOrderByCreateTime(Integer type) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Material::getType, type)
                .eq(Material::getDeleted, 0)
                .orderByDesc(Material::getCreateTime);
        return selectList(queryWrapper);
    }

    /**
     * 查询价格范围内的物料
     */
    default List<Material> selectByPriceRange(Double minPrice, Double maxPrice) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(minPrice != null, Material::getPrice, minPrice)
                .le(maxPrice != null, Material::getPrice, maxPrice)
                .eq(Material::getDeleted, 0)
                .orderByAsc(Material::getPrice);
        return selectList(queryWrapper);
    }

    /**
     * 根据物料名称模糊查询
     */
    default List<Material> selectByNameLike(String name) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Material::getName, name)
                .eq(Material::getDeleted, 0);
        return selectList(queryWrapper);
    }

    /**
     * 查询启用的物料
     */
    default List<Material> selectEnabledMaterials() {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Material::getEnabled, true)
                .eq(Material::getDeleted, 0)
                .orderByDesc(Material::getUpdateTime);
        return selectList(queryWrapper);
    }

    /**
     * 根据类型和库存数量查询
     */
    default List<Material> selectByTypeAndStock(Integer type, Integer minStock) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null, Material::getType, type)
                .ge(minStock != null, Material::getStock, minStock)
                .eq(Material::getDeleted, 0);
        return selectList(queryWrapper);
    }
}